package com.gome.controller;

import com.gome.entity.User;
import com.gome.service.UserService;
import com.gome.utils.Result;
import com.gome.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jservices/user")
public class UserController  {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value="/queryUserLikeOa",method =RequestMethod.POST)
    public Result queryUserLikeOa(@RequestParam String ocOazh, HttpServletRequest req){
        Map<String,Object> map= new HashMap<String,Object>();
        if(null!=ocOazh&&!"".equals(ocOazh)){
            map.put("oc_oazh", "%"+ocOazh+"%");
        }
        List<User> users = userService.selectUserByMap(map);
        System.out.println(req.getSession().getId());
        return  new Result(ResultCode.SUCCESS, null, users);
    }

    @ResponseBody
    @RequestMapping(value="/queryOaUserByZh",method =RequestMethod.POST)
    public Result  queryOaUserByZh(@RequestParam String ocOazh){

        return   userService.queryOaUserByZh(ocOazh);
    }

    @Transactional
    @ResponseBody
    @RequestMapping(value="/addUser",method =RequestMethod.POST)
    public Result  addUser(@RequestParam User user){
        user.setOcCzy("admin");
        return   userService.addUser(user);
    }

    @Transactional
    @ResponseBody
    @RequestMapping(value="/updateUser",method =RequestMethod.POST)
    public Result  updateUser(@RequestParam User user){
        user.setOcCzy("admin");
        return   userService.updateUser(user);
    }
}
